package com.redoop;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Properties;
import java.util.stream.Stream;

public class CreateDataXJson {

	public static void main(String[] args) throws IOException, Exception {
		FileReader fileReader = new FileReader("oracle.txt");

		BufferedReader br = new BufferedReader(fileReader);

		Properties p = new Properties();

		try {
			BufferedReader bufferedReader = new BufferedReader(new FileReader("jdbc.properties"));

			p.load(bufferedReader);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String jdbcType = p.getProperty("mysql.jdbcType");
		String jdbcDriver = p.getProperty("mysql.jdbcDriver");
		String jdbcURL = p.getProperty("mysql.jdbcURL");
		String DataBase = p.getProperty("mysql.database");
		String jdbcUserName = p.getProperty("mysql.username");
		String jdbcPassWord = p.getProperty("mysql.password");
		String hdfsURL = p.getProperty("hdfsURL");
		String savePath = p.getProperty("savepath");

		Connection conn = null;
		ResultSet rs = null;
		FileWriter fw = null;
		BufferedWriter bw = null;

		try {
			Class.forName(jdbcDriver);
			conn = DriverManager.getConnection(jdbcURL, jdbcUserName, jdbcPassWord);

			DatabaseMetaData dbmd = conn.getMetaData();
			for (int i = 1; i < getCount(); i++) {
				// System.out.println(br.readLine());
				String readLine = br.readLine().replaceAll(" ", "");
				System.out.println(readLine);
				fw = new FileWriter(readLine + ".json");
				bw = new BufferedWriter(fw);
				rs = dbmd.getColumns(null, null, readLine, null);

				HashSet<String> h = new LinkedHashSet<>();
				String DataXQ = "{\"job\": {\"setting\": {\"speed\": {\"channel\":1}},\"content\": [{\"reader\": {\"name\": \""
						+ jdbcType + "\",\"parameter\": {\"username\": \"" + jdbcUserName + "\",\"password\": \""
						+ jdbcPassWord
						+ "\",\"connection\": [{\"querySql\": [\"select * from "+DataBase+"."+readLine+"\"],\"jdbcUrl\": [\""
						+ jdbcURL + "\",]}]}},\"writer\": {\"name\": \"hdfswriter\",\"parameter\": {\"defaultFS\": \""
						+ hdfsURL + "\",\"fileType\": \"text\",\"path\": \"" + savePath
						+ "\",\"fileName\": \""+readLine+"\",\"column\": [";

				String DataXH = "],\"writeMode\": \"append\",\"fieldDelimiter\": \"\\t\",}}}]}}";
				while (rs.next()) {

					String columName = rs.getString("COLUMN_NAME");
					String columType = rs.getString("TYPE_NAME");
					
					if(columType.equals("VARCHAR2")|columType.equals("VARCHAR")| columType.equals("CHAR")| columType.equals("NUMBER")){
						columType="string";
					}else if (columType.equals("TIMESTAMP")){
						columType="Date";
					}
					// String sql=columnane+" "+columtype+","+"\n";//拼接sql使用
					String json = "{\"name\":\"" + columName + "\",\"type\":\"" + columType + "\"},";// 拼接dataX的json文件使用
					h.add(json);
					// bw.write(json + "\n");

					// bw.write(columnane+" "+columtype+"\n");

				}
				String DataXJ="";
				for (String string : h) {
					DataXJ=DataXJ+string;
					//sbw.write(DataXQ+string+DataXH + "\n");
				}
				bw.write(DataXQ+DataXJ+DataXH);

				System.out.println("--------------------------------------------");

				fw.flush();
				bw.flush();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			bw.close();
			fw.close();
			rs.close();
			conn.close();
			br.close();
		}

	}

	public static long getCount() throws FileNotFoundException {
		FileReader fileReader = new FileReader("oracle.txt");
		BufferedReader br = new BufferedReader(fileReader);
		Stream<String> lines = br.lines();

		long count = lines.count();
		// System.out.println("一共读取到" + count + "行的数据，请验证后决定是否复制粘贴");
		return count;
	}

}
